package com.team7.happycommunity.gatway.filter;

import com.alibaba.fastjson.JSONObject;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import com.team7.happycommunity.gatway.common.CommonResult;
import com.team7.happycommunity.gatway.utils.JWTUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
import org.springframework.stereotype.Component;
/**
 * 个人中心
 */
@Component
public class PersoncentorSecurityFilter  extends ZuulFilter {

    @Autowired
    JWTUtils jwtUtils;

    /**
     * 运行阶段
     * pre、route、post、error
     * @return
     */
    @Override
    public String filterType() {
        return FilterConstants.PRE_TYPE;
    }

    @Override
    public int filterOrder() {
        return 0;
    }

    /**
     * 开关控制
     * @return
     */
    @Override
    public boolean shouldFilter() {
//        System.out.println(RequestContext.getCurrentContext().getRequest().getRequestURI());
        // 测试 路径
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personcentor/personUser/test")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/uploadHeadImage")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/myAttentionDynamic")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/myJoinAcitivity")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/myPublishAcitivity")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/dynamic")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/exit")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/personCenter")){
            return true;
        }
        if (RequestContext.getCurrentContext().getRequest().getRequestURI().contains("/personUser/resetPasswordBypassword")){
            return true;
        }
        return false;
    }



    @Override
    public Object run() throws ZuulException {

        RequestContext requestContext = RequestContext.getCurrentContext();
        // 获取 用户名信息
        String username = requestContext.getRequest().getHeader("userName");
        String token = requestContext.getRequest().getHeader("token");
       String userId = requestContext.getRequest().getHeader("userId");
        System.out.println("username"+username);
        System.out.println("userId"+userId);
        System.out.println("token"+token);
       //requestContext.
       requestContext.addZuulRequestHeader("userId",userId);
        requestContext.addZuulRequestHeader("userName",username);


                // 解密 token 信息
        String result_name = jwtUtils.unsign(token,String.class);
        System.out.println("result_name"+result_name);
        if(!result_name.equals(username)){
            // 认证失败拒绝访问
            requestContext.setSendZuulResponse(false);
            requestContext.setResponseStatusCode(200);
            JSONObject jsonObject = (JSONObject) JSONObject.toJSON(CommonResult.failed("登录失败"));
            requestContext.setResponseBody(jsonObject.toJSONString());
        }
        return null;
    }
}
